﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL_MechSys;
using DTO_MechSys;

namespace FORMS_MechSys
{
    public partial class frmVeiculoSelecionar : Form
    {
        BLLVeiculo bllVeiculo = new BLLVeiculo();
        List<DTOVeiculo> ListVeiculos;

        DTOCor dtoCorPrincipal = new DTOCor();
        DTOCor dtoCorFundo = new DTOCor();
        DTOCor dtoCorLetra = new DTOCor();
        DTOCor dtoCorDataGrid = new DTOCor();

        BLLCor bllCor = new BLLCor();

        
        

        public frmVeiculoSelecionar()
        {
            InitializeComponent();
            PopulaGrid();
            SetColors();
        }

        private void SetColors()
        {
            dtoCorPrincipal = bllCor.Buscar("Principal");
            toolStrip1.BackColor = tableLayoutPanel5.BackColor = label4.BackColor =
            btnNovoVeiculo.BackColor = btnPesquisar.BackColor = dtoCorPrincipal.Cor;

            dtoCorFundo = bllCor.Buscar("Fundo");
            tableLayoutPanel1.BackColor = dtoCorFundo.Cor;

            dtoCorDataGrid = bllCor.Buscar("DataGrid");
            dtGridSelecionaVeiculo.BackgroundColor = dtoCorDataGrid.Cor;

            dtoCorLetra = bllCor.Buscar("Letra");
            btnNovoVeiculo.ForeColor = label17.ForeColor = toolStripButtonSair.ForeColor = label4.ForeColor = dtoCorLetra.Cor;


        }
        public void PopulaGrid()
        {
            DataRow dataRow;
            DataTable dataTableGridView = new DataTable();
            ListVeiculos = bllVeiculo.BuscarVeiculosCadastrados();
            try
            {
                if (ListVeiculos.Count > 0)
                {
                    dataTableGridView.Columns.Add("VeiculoID", typeof(int));
                    dataTableGridView.Columns.Add("Placa", typeof(string));
                    dataTableGridView.Columns.Add("Marca", typeof(string));
                    dataTableGridView.Columns.Add("Modelo", typeof(string));
                    dataTableGridView.Columns.Add("AnoFab", typeof(string));
                    dataTableGridView.Columns.Add("AnoMod", typeof(string));


                    foreach (DTOVeiculo dtoVeiculo in ListVeiculos)
                    {
                        dataRow = dataTableGridView.NewRow();

                        dataRow["VeiculoID"] = dtoVeiculo.VeiculoID;
                        dataRow["Placa"] = dtoVeiculo.Placa;
                        dataRow["Marca"] = dtoVeiculo.Marca;
                        dataRow["Modelo"] = dtoVeiculo.Modelo;
                        dataRow["AnoFab"] = dtoVeiculo.AnoFab;
                        dataRow["AnoMod"] = dtoVeiculo.AnoMod;

                        // Adiciona linha ao DataTable.
                        dataTableGridView.Rows.Add(dataRow);

                    }
                }

                dtGridSelecionaVeiculo.DataSource = dataTableGridView;
            }
            catch (Exception)
            {
                if (new frmMessageBox().Show("Não foi possível carregar os dados para o grid!", "Carregamento", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
                {
                    PopulaGrid();
                }
            }
           
        }

        public void PopulaGrid(string texto)
        {
            DataRow dataRow;
            DataTable dataTableGridView = new DataTable();
            ListVeiculos = bllVeiculo.PesquisarVeiculos(texto);
            try
            {
                if (ListVeiculos.Count > 0)
                {
                    dataTableGridView.Columns.Add("VeiculoID", typeof(int));
                    dataTableGridView.Columns.Add("Placa", typeof(string));
                    dataTableGridView.Columns.Add("Marca", typeof(string));
                    dataTableGridView.Columns.Add("Modelo", typeof(string));
                    dataTableGridView.Columns.Add("AnoFab", typeof(string));
                    dataTableGridView.Columns.Add("AnoMod", typeof(string));


                    foreach (DTOVeiculo dtoVeiculo in ListVeiculos)
                    {
                        dataRow = dataTableGridView.NewRow();

                        dataRow["VeiculoID"] = dtoVeiculo.VeiculoID;
                        dataRow["Placa"] = dtoVeiculo.Placa;
                        dataRow["Marca"] = dtoVeiculo.Marca;
                        dataRow["Modelo"] = dtoVeiculo.Modelo;
                        dataRow["AnoFab"] = dtoVeiculo.AnoFab;
                        dataRow["AnoMod"] = dtoVeiculo.AnoMod;

                        // Adiciona linha ao DataTable.
                        dataTableGridView.Rows.Add(dataRow);

                    }
                }

                dtGridSelecionaVeiculo.DataSource = dataTableGridView;
            }
            catch (Exception)
            {
                new frmMessageBox().Show("Registro não encontrado!", "Pesquisa", MessageBoxButtons.OK, MessageBoxIcon.Information);               
            }

        }

        private void frmVeiculoSelecionar_Load(object sender, EventArgs e)
        {

        }
      
        private void dtGridSelecionaVeiculo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                string marca = (string)dtGridSelecionaVeiculo.Rows[e.RowIndex].Cells["Marca"].Value;
                string modelo = (string)dtGridSelecionaVeiculo.Rows[e.RowIndex].Cells["Modelo"].Value;
                BLLUtilitarios.veiculoNome = string.Format("{0} {1}", marca, modelo);
                BLLUtilitarios.veiculoID = (int)dtGridSelecionaVeiculo.Rows[e.RowIndex].Cells["VeiculoID"].Value;
                this.Close();
            }
        }

        private void toolStripButtonSair_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPesquisar.Text))
            {
                PopulaGrid(txtPesquisar.Text);
            }
        }

        private void btnNovoVeiculo_Click(object sender, EventArgs e)
        {
            frmVeiculoCadastro frmveiculoCadastro = new frmVeiculoCadastro();
            frmveiculoCadastro.ShowDialog();
        }
    }
}
